extends Node

const config_path = "user://version_number.cfg"
const suffix_options = [
	"Base",
	"Alpha",
	"Beta",
	"RC",
	"Release"
]

var config_datas = {}

const global_section_key = "GlobalSetting"
const default_project_name_key = "DefaultProjectName"
var default_project_name = ""

func read_config():
	config_datas.clear()
	var config = ConfigFile.new()
	config.load(config_path)
	default_project_name = config.get_value(global_section_key, default_project_name_key, "")
	
	var sections = config.get_sections()
	for a in sections:
		if a == global_section_key:
			continue
			
		var configData = ConfigData.new()
		configData.read_config(a)
		config_datas[a] = configData
		
		if default_project_name == "":
			default_project_name = configData.project_name
	
	if default_project_name == "":
		default_project_name = "默认项目"
	if config_datas.size() == 0:
		var defaultConfigData = ConfigData.new()
		defaultConfigData.d = Time.get_date_string_from_system()
		defaultConfigData.d = defaultConfigData.d.replace("-", "")
		defaultConfigData.project_name = default_project_name
		defaultConfigData.e = 2
		var userPath = ProjectSettings.globalize_path("user://")
		defaultConfigData.save_path = userPath
		defaultConfigData.save_config()
		config_datas[default_project_name] = defaultConfigData


func save_config():
	for key in config_datas.keys():
		var data = config_datas[key]
		data.save_config()


func save_default_select_project_name(project_name):
	default_project_name = project_name
	var config = ConfigFile.new()
	config.load(config_path)
	config.set_value(global_section_key, default_project_name_key, default_project_name)
	config.save(config_path)


func add_project(project_name):
	var config = ConfigData.new()
	config.read_config(project_name)
	config.save_config()
	config_datas[project_name] = config


func remove_project(project_name):
	var config = ConfigFile.new()
	config.load(config_path)
	if config.has_section(project_name):
		config.erase_section(project_name)
	config_datas.erase(project_name)
	config.save(config_path)


func rename_project(old_name, new_name):
	if old_name == new_name:
		return
	
	if not config_datas.has(old_name):
		return
		
	var data = config_datas[old_name]
	data.project_name = new_name
	config_datas.erase(old_name)
	config_datas[new_name] = data
	data.save_config()
		
	var config = ConfigFile.new()
	config.load(config_path)
	if not config.has_section(old_name):
		return
	config.erase_section(old_name)
	config.save(config_path)


func update_save_path(project_name, new_save_path):
	if not config_datas.has(project_name):
		return
	var data = config_datas[project_name]
	data.save_path = new_save_path
	data.save_config()


func _ready() -> void:
	read_config()


class ConfigData:
	var project_name = ""
	var a = 0
	var b = 0
	var c = 0
	var d = ""
	var e = 0
	var save_path = ""
	
	const key_a = "A"
	const key_b = "B"
	const key_c = "C"
	const key_d = "D"
	const key_e = "E"
	const key_save_path = "SavePath"
	
	
	func read_config(sectionName):
		project_name = sectionName
		var config = ConfigFile.new()
		config.load(config_path)
		a = config.get_value(project_name, key_a, a)
		b = config.get_value(project_name, key_b, b)
		c = config.get_value(project_name, key_c, c)
		var dateStr = Time.get_date_string_from_system()
		var defaultD = dateStr.replace("-", "")
		d = config.get_value(project_name, key_d, defaultD)
		e = config.get_value(project_name, key_e, 2)
		var system_folder = ProjectSettings.globalize_path("user://")
		save_path = config.get_value(project_name, key_save_path, system_folder)
		
	
	func save_config():
		var config = ConfigFile.new()
		config.load(config_path)
		config.set_value(project_name, key_a, a)
		config.set_value(project_name, key_b, b)
		config.set_value(project_name, key_c, c)
		d = Time.get_date_string_from_system()
		d = d.replace("-", "")
		config.set_value(project_name, key_d, d)
		config.set_value(project_name, key_e, e)
		config.set_value(project_name, key_save_path, save_path)
		config.save(config_path)
		
		
	func get_version_str(useCurrentData:bool = false):
		var tempD = d
		if useCurrentData:
			tempD = Time.get_date_string_from_system()
			tempD = tempD.replace("-", "")
			
		var values = {
			"a": a, 
			"b": b,
			"c": c,
			"d": tempD,
			"e": suffix_options[e],
		}
		var versionStr = "{a}.{b}.{c}.{d}_{e}".format(values)
		return versionStr
